#
#    Copyright (C) 2016 Francisco Javier Parra <franparpe@openmailbox.org>
#
#    This program is free software; you can redistribute it and/or modify
#    it under the terms of the GNU General Public License as published by
#    the Free Software Foundation; either version 2 of the License, or
#    (at your option) any later version.
#
#    This program is distributed in the hope that it will be useful,
#    but WITHOUT ANY WARRANTY; without even the implied warranty of
#    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
#    GNU General Public License for more details.
#
#    You should have received a copy of the GNU General Public License
#    along with this program; if not, write to the Free Software
#    Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA  02110-1301 USA
#

from PyQt5.QtWidgets import QFrame, QComboBox
from PyQt5.QtWidgets import QGroupBox
from PyQt5.QtWidgets import QGridLayout

from PyQt5 import QtCore

try:
    _fromUtf8 = QtCore.QString.fromUtf8
except AttributeError:
    def _fromUtf8(s):
        return s


class CamFollowerType(QFrame):
    """The box that defines the mechanism
    type with it combobox
    """
    def __init__(self, parent):
        "Initializing"
        super(CamFollowerType, self).__init__(parent)
        self.setFrameShape(self.StyledPanel)
        self.setFrameShadow(self.Raised)
        self.grid = QGridLayout(self)
        self.groupBox = QGroupBox(self)
        self.gBTitle = _fromUtf8("Type of cam-follower mechanism")
        self.groupBox.setTitle(self.gBTitle)
        self.grid.addWidget(self.groupBox, 0, 0, 1, 1)

        # The cam-follower type selector
        self.camTypeSelector = QComboBox(self.groupBox)
        self.camTypeSelectorContents()
        self.Grid4groupBox = QGridLayout(self.groupBox)
        self.Grid4groupBox.addWidget(self.camTypeSelector, 0, 0, 1, 1)

    def camTypeSelectorContents(self):
        """Initialize the comboBox contents
        """
        unilateral = _fromUtf8("cam-follower unilateral contact")
        bilateral = _fromUtf8("cam-follower bilateral contact")
        bilateralConstWidth = _fromUtf8("cam-follower bilateral with constant width")
        self.camTypeSelector.addItem(unilateral)
        self.camTypeSelector.addItem(bilateral)
        self.camTypeSelector.addItem(bilateralConstWidth)

    def getMechanismType(self):
        """Returns a the type of mechanism as a string
        """
        return self.camTypeSelector.currentText()

    def connect(self, function):
        """connect the action with the
        comboBox
        """
        self.camTypeSelector.currentIndexChanged.connect(function)
